package com.aiti.lulian.controller;
/**
 * @Author chenxianzhen
 * @create 2024/6/18 16:13
 */

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.base.core.utils.obj.BeanCopyUtil;
import com.aiti.lulian.dto.SupplierSelectDto;
import com.aiti.lulian.entity.LuLianCgsOrder;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.LuLianSupplier;
import com.aiti.lulian.entity.SysArea;
import com.aiti.lulian.service.ILuLianEnterpriseService;
import com.aiti.lulian.service.ILuLianSupplierService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName LuLianSupplierController
 * @Description TODO
 * @Author chenxianzhen
 * @Date 2024/6/18 16:13
 */
@RestController
@RequestMapping("/supplier")
public class LuLianSupplierController extends WebController {
    @Resource
    ILuLianSupplierService luLianSupplierService;
    @Resource
    ILuLianEnterpriseService luLianEnterpriseService;
    @GetMapping("/findPage")
    public Message findPage(SupplierSelectDto supplierSelectDto) {
        try{
            Integer pageNum = supplierSelectDto.getPageNum();
            Integer pageSize = supplierSelectDto.getPageSize();

            Page<LuLianSupplier> page = startPage(pageNum,pageSize);
            QueryWrapper<LuLianSupplier> wrapper = new QueryWrapper<LuLianSupplier>();
            LuLianSupplier luLianSupplier = BeanCopyUtil.convertBean(supplierSelectDto, LuLianSupplier.class);
            IPage<LuLianSupplier> pageResult = luLianSupplierService.findPage(page, supplierSelectDto);
            List<LuLianSupplier> list = pageResult.getRecords();
            //获取活动企业信息
            for (int i = 0; i < list.size(); i++) {
                String enterpriseId = list.get(i).getEnterpriseId();
                LuLianEnterprise enterprise = luLianEnterpriseService.findByBaseId(enterpriseId);
            //    获取企业活动订单
                List<LuLianCgsOrder> orders =  luLianEnterpriseService.findActivityOrderByBaseId(enterpriseId);
                enterprise.setOrders(orders);
                list.get(i).setEnterprise(enterprise);
            }
            return Success(getPageResult(list,pageNum,pageSize,(int)page.getTotal()));
        }catch (Exception e){
            return Error(e.getMessage());
        }
    }
}
